//
//  UINavigationBar+Addition.m
//  Epweike_Witkey
//
//  Created by Mac on 15/7/29.
//  Copyright (c) 2015年 zengxusheng. All rights reserved.
//

#import "UINavigationBar+Addition.h"

@implementation UINavigationBar(Addition)

#pragma mark - UINavigationBar Buttons.
#pragma mark Back Button.
+ (void)customNavBackBtnWithViewCtrller:(UIViewController *)currentViewCtrller target:(id)btnTarget action:(SEL)btnAction
{
    if (IS_IOS7)
        [UINavigationBar customNavBackBtnWithViewCtrller:currentViewCtrller withTitle:@"  " target:btnTarget action:btnAction];
    else
        [UINavigationBar customNavBackBtnWithViewCtrller:currentViewCtrller withTitle:@"  " target:btnTarget action:btnAction];
}

+ (void)customNavBackBtnWithViewCtrller:(UIViewController *)currentViewCtrller withTitle:(NSString *)title target:(id)btnTarget action:(SEL)btnAction
{
    UIButton *backBtn = [UINavigationBar backButtonWithTextOS7Style:title];
    if ([btnTarget respondsToSelector:btnAction])
        [backBtn addTarget:btnTarget action:btnAction forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backBarItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    currentViewCtrller.navigationItem.leftBarButtonItem = backBarItem;
}

#pragma mark Left Button
+ (UIButton *)customNavCtrlLeftItemWithNotableBtn:(UIViewController *)viewCtrl title:(NSString *)title
{
    UIButton *leftBtn = [self notableButtonForNavigationBarWithTitle:title];
    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [viewCtrl.navigationItem setLeftBarButtonItem:leftBarBtnItem];
    
    return leftBtn;
}

+ (UIButton *)customNavCtrlLeftItemWithDefaultBtn:(UIViewController *)viewCtrl title:(NSString *)title
{
    UIImage *navBtnImg = [UIImage new];
    UIImage *navBtnImgHighlight = [UIImage new];
    
    UIButton *leftBtn = [self navBtnWithImg:navBtnImg highlightImg:navBtnImgHighlight title:title];
    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [viewCtrl.navigationItem setLeftBarButtonItem:leftBarBtnItem];
    
    return leftBtn;
}

#pragma mark Right Button
+ (UIButton *)customNavCtrlRightItemWithNotableBtn:(UIViewController *)viewCtrl title:(NSString *)title
{
    UIButton *rightBtn = [self notableButtonForNavigationBarWithTitle:title];
    UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [viewCtrl.navigationItem setRightBarButtonItem:rightBarBtnItem];
    
    return rightBtn;
}

+ (UIButton *)customNavCtrlRightItemWithDefaultBtn:(UIViewController *)viewCtrl title:(NSString *)title
{
    UIImage *navBtnImg = [UIImage new];
    UIImage *navBtnImgHighlight = [UIImage new];
    
    UIButton *rightBtn = [self navBtnWithImg:navBtnImg highlightImg:navBtnImgHighlight title:title];
    UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [viewCtrl.navigationItem setRightBarButtonItem:rightBarBtnItem];
    
    return rightBtn;
}

+ (UIButton *)customNavCtrlLeftItemWithBgImgName:(NSString *)bgImgName highlightBgImgName:(NSString *)highlightBgImgName targetViewCtrl:(UIViewController *)viewCtrl
{
    UIImage *iconImg = [UIImage imageNamed:bgImgName];
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, iconImg.size.width, iconImg.size.height)];
    [leftBtn setImage:[UIImage imageNamed:bgImgName] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:highlightBgImgName] forState:UIControlStateHighlighted];
    
    UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [viewCtrl.navigationItem setLeftBarButtonItem:leftBarBtnItem];
    
    return leftBtn;
}

+ (UIButton *)customNavCtrlRightItemWithBgImgName:(NSString *)bgImgName highlightBgImgName:(NSString *)highlightBgImgName targetViewCtrl:(UIViewController *)viewCtrl
{
    UIImage *iconImg = [UIImage imageNamed:bgImgName];
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, iconImg.size.width, iconImg.size.height)];
    [rightBtn setBackgroundImage:[UIImage imageNamed:bgImgName] forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageNamed:highlightBgImgName] forState:UIControlStateHighlighted];
    
    UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [viewCtrl.navigationItem setRightBarButtonItem:rightBarBtnItem];
    
    return rightBtn;
}

//导航栏右侧双按钮（一个图片，一个文字）
+ (void)customNavCtrlTwoRightItemWithBgImgName:(NSString *)bgImgName titleName:(NSString *)titleName targetViewCtrl:(UIViewController *)viewCtrl
{
    UIImage *iconImg = [UIImage imageNamed:bgImgName];
    UIBarButtonItem *secondRightBtn = [[UIBarButtonItem alloc] initWithImage:iconImg style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(secondRightBarButtonItemClick:)];
    secondRightBtn.tintColor = [UIColor whiteColor];
    
    UIBarButtonItem *firstRightBtn = [[UIBarButtonItem alloc] initWithTitle:titleName style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(firstRightBarButtonItemClick:)];
    firstRightBtn.tintColor = [UIColor whiteColor];
    
    NSArray *buttonArray = [[NSArray alloc]initWithObjects:firstRightBtn, secondRightBtn, nil];
    [viewCtrl.navigationItem setRightBarButtonItems:buttonArray];
    
    return;
}

//导航栏右侧按钮（一个文字,一个图片）
+ (void)customNavCtrlRightItemWithTitleName:(NSString *)titleName bgImgName:(NSString *)bgImgName  targetViewCtrl:(UIViewController *)viewCtrl
{
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 80, 44);
    [rightBtn setImage:[UIImage imageNamed:bgImgName] forState:UIControlStateNormal];
    [rightBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 65, 0, 0)];
    [rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 12)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [rightBtn setTitle:titleName forState:UIControlStateNormal];
    rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightBtn addTarget:viewCtrl action:@selector(rightBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *firstRightBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    [viewCtrl.navigationItem setRightBarButtonItem:firstRightBtn];
    
    return;
}

//导航栏右侧双按钮（双图片）
+ (void)customNavCtrlTwoRightItemWithFirstBgImgName:(NSString *)firstBgImgName secondBgImgName:(NSString *)secondBgImgName targetViewCtrl:(UIViewController *)viewCtrl
{
    UIImage *iconImg = [UIImage imageNamed:firstBgImgName];
    UIBarButtonItem *firstRightBtn = [[UIBarButtonItem alloc] initWithImage:iconImg style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(firstRightBarButtonItemClick:)];
    firstRightBtn.tintColor = [UIColor whiteColor];
    
    UIImage *iconImg2 = [UIImage imageNamed:secondBgImgName];
    UIBarButtonItem *secondRightBtn = [[UIBarButtonItem alloc] initWithImage:iconImg2 style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(secondRightBarButtonItemClick:)];
    secondRightBtn.tintColor = [UIColor whiteColor];
    
    NSArray *buttonArray = [[NSArray alloc]initWithObjects:firstRightBtn, secondRightBtn, nil];
    [viewCtrl.navigationItem setRightBarButtonItems:buttonArray];
    
    return;
}

#pragma mark - Private
+ (UIButton *)notableButtonForNavigationBarWithTitle:(NSString *)title
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    button.layer.cornerRadius = 4.f;
    button.layer.borderWidth = 0.5f;
    button.layer.borderColor = [UIColor colorWithWhite:1.f alpha:.5f].CGColor;
    button.layer.masksToBounds = YES;
    [button setContentEdgeInsets:UIEdgeInsetsMake(5, 12, 5, 12)];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:1.f alpha:.6f]] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:1.f alpha:.4f]] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:1.f alpha:.25f]] forState:UIControlStateDisabled];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:kNavBarButtonTitleFont];
    [button setTitleColor:[UIColor colorWithHexString:@"0xff85"] forState:UIControlStateNormal];
    [button sizeToFit];
    return button;
}

+ (UIButton *)navBtnWithImg:(UIImage *)image highlightImg:(UIImage *)highlightImg title:(NSString *)title
{
    CGFloat btnWidth = 0, btnHeight = 31;
    UIButton *navBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, btnWidth, btnHeight)];
    [navBtn setBackgroundImage:image forState:UIControlStateNormal];
    [navBtn setBackgroundImage:highlightImg forState:UIControlStateHighlighted];
    
    [navBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [navBtn setTitleColor:[UIColor colorWithWhite:1.f alpha:.7f] forState:UIControlStateHighlighted];
    [navBtn setTitleColor:[UIColor colorWithWhite:1.f alpha:.3f] forState:UIControlStateDisabled];
    
    if (!IS_IOS7)
        [navBtn setContentEdgeInsets:UIEdgeInsetsMake(5, 12, 5, 12)];
    
    [navBtn.titleLabel setFont:kNavBarButtonTitleFont];
    [navBtn setTitle:title forState:UIControlStateNormal];
    [navBtn sizeToFit];
    
    return navBtn;
}

+ (UIButton *)backButtonWithTextOS7Style:(NSString *)backText
{
    UIImage *backBtnBg = [UIImage imageNamed:@"navigationbar_back_btn"];
    UIImage *backBtnHighlightBg = [UIImage imageNamed:@"navigationbar_back_btn_highlight"];
    
    UIButton *button = [UINavigationBar backButtonOS7StyleWithText:backText normalImg:backBtnBg highlightImg:backBtnHighlightBg];
    return button;
}

+ (UIButton *)backButtonOS7StyleWithText:(NSString *)backText normalImg:(UIImage *)normalImg highlightImg:(UIImage *)highlightImg
{
    UIButton *custBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    custBackBtn.titleLabel.font = kNavBarButtonTitleFont;
    [custBackBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [custBackBtn setTitleColor:[UIColor colorWithWhite:1.f alpha:0.5f] forState:UIControlStateHighlighted];
    if (IS_IOS7) {
        [custBackBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [custBackBtn setTitleEdgeInsets:UIEdgeInsetsMake(2, 0, 0, -12)];
    } else {
        [custBackBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    }
    [custBackBtn setTitle:backText forState:UIControlStateNormal];
    [custBackBtn setImage:normalImg forState:UIControlStateNormal];
    [custBackBtn setImage:highlightImg forState:UIControlStateHighlighted];
    [custBackBtn sizeToFit];
    return custBackBtn;
}

@end

